/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/

/**
 * OC is the core module of the whole project. It is responsible for the configuration of the
 * running environment and load the jQuery module.
 * @module OC
 * @requires jQuery
 * @example
 *     var OC = require("./oc"); //How to require OC module.
 */
define(function (require, exports, module) {
	'use strict';
	var config,
		$ = require("lib/jquery/jquery"),
		token = function () {
			var token = window.location.href;
			return token.match(/([a-zA-Z0-9]+)\.html/)[1];
		};
	//TODO test token
	token = "EAF65B6D98F0135E";
	/**
	 * Runtime environment configure.
	 * @class config
	 * @static
	 */
	config = {
		"CURRENT_LANG": null,
		/**
		 * Set loop times that you want in default.
		 * @property DEFAULT_MAX_LOOPS
		 * @type number
		 * @default 2000
		 */
		"DEFAULT_MAX_LOOPS": 2000,
		/**
		 * User can define to use this delay.When new DelayStep.
		 * @property DEFAULT_DELAY
		 * @type number
		 * @default 4000
		 */
		"DEFAULT_DELAY": 4000,
		"DEFAULT_LANG": "zh_CN",
		/**
		 * This setting just affect delay model.
		 * @property DELAY_RATIO
		 * @type number
		 * @default 1.0
		 */
		"DELAY_RATIO": 1.0,
		/**
		 * Enable / Disable the mode of ratio of delay.
		 * @property DELAY_RATIO_MODE
		 * @type boolean
		 * @default false
		 */
		"DELAY_RATIO_MODE": false,
		/**
		 * If the value is true, script will overriding function alert() in the iframe.window object.
		 * @property DISABLE_ALERT
		 * @type boolean
		 * @default false
		 */
		"DISABLE_ALERT": false,
		/**
		 * Open Edit mode.
		 * @property EDIT_MODE
		 * @type boolean
		 * @default true
		 */
		"EDIT_MODE": true,
		/**
		 * Define how frequent to test a event.
		 * @property EVENT_TEST_CYCLE
		 * @type number
		 * @default 50
		 */
		"EVENT_TEST_CYCLE": 50,
		/**
		 * Define how long to determine the failure.
		 * @property EVENT_TIMEOUT
		 * @type number
		 * @default 10000
		 */
		"EVENT_TIMEOUT": 10000,
		/**
		 * If use focus "stepby" mode, all method of step will be lose efficacy.
		 * @property FOCUS_STEPBY
		 * @type boolean
		 * @default false
		 */
		"FOCUS_STEPBY": false,
		/**
		 * How long will the highlight div disappeared.
		 * @property HIGHLIGHT_REMOVE_DELAY
		 * @type number
		 * @default 2000
		 */
		"HIGHLIGHT_REMOVE_DELAY": 2000,
		/**
		 * Highlight the DOM of operation when doing test.
		 * @property SHOW_AREA_MODE
		 * @type boolean
		 * @default true
		 */
		"SHOW_AREA_MODE": true,
		/**
		 * Define the title of probe.html
		 * @property TITLE
		 * @type string
		 * @default PROBE
		 */
		"TITLE": "PROBE",
		/**
		 * Define the url of Test Management Server.
		 * @property TEST_SERVER
		 * @type string
		 */
		"TEST_SERVER": "http://testweb.oschina.mopaas.com/",
		"DATA_SERVER": "http://testweb.oschina.mopaas.com/json/",
		/**
		 * Define the url of Test Management Server.
		 * @property TEST_SCRIPT_PATH
		 * @type string
		 */
		"TEST_SCRIPT_PATH": "",

		// TOKEN from window.location.

		"UI_CONSOLE_OPEN": true
	};
	/**
	 * Return setting item by key.
	 * @method cfg
	 * @param {string} key
	 * @param {string} [value]
	 * @returns {string|number|boolean} value.
	 * @example
	 *     OC.cfg("DEFAULT_DELAY"); //Query the value of a key.
	 * @example
	 *     OC.cfg("DEFAULT_DELAY", 6000); //Set a value of a key.
	 */
	exports.cfg = function (key, value) {
		if (typeof (value) === "undefined") {
			return config[key];
		} else {
			config[key] = value;
			return value;
		}
	};
	/**
	 * Batch setting the runtime environment
	 * @method fanIn
	 * @param {object} object
	 * @example
	 *     OC.fanIn({DEFAULT_DELAY:6000,EVENT_TIMEOUT:10000});
	 */
	exports.fanIn = function (object) {
		var key;
		for (key in object) {
			if (object.hasOwnProperty(key) && config.hasOwnProperty(key)) {
				config[key] = object[key];
			}
		}
	};
	/**
	 * Output json of the runtime environment
	 * @method fanOut
	 * @returns {string} info
	 * @example
	 *     OC.fanIn(); //To return json string of config
	 */
	exports.fanOut = function () {
		var key, info = "{",
			len;
		for (key in config) {
			if (config.hasOwnProperty(key)) {
				info += "\"" + key + "\":\"" + config[key] + "\",";
			}
		}
		len = info.length;
		info = info.substr(0, len - 1);
		info += "}";
		return info;

	};
	/**
	 * Get the value of token.
	 * @method getToken
	 * @returns {string} token
	 * @example
	 *     OC.getToken(); //To return string of token
	 */ 
	exports.getToken = function () {
		return token;
	};
	/**
	 * Get some infomation or execute some actions from the url address.
	 * @method pull
	 * @param url 
	 * @param callback {function}
	 * @example
	 *     OC.pull("/testweb/json/lang_zh_CN.json",function(){}); 
	 */
	exports.pull = function (url, callback) {
		$.ajax({
			url: url,
			dataType: 'jsonp',
			jsonp: 'callback',
			jsonpCallback: 'jsonpcallback',
			success: callback
		});
	};
});